package com.lizonghui.googleplay.fragment;

import android.os.SystemClock;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import com.lizonghui.googleplay.adapter.ItemAdapter;
import com.lizonghui.googleplay.base.BaseFragment;
import com.lizonghui.googleplay.base.LoadingPager;
import com.lizonghui.googleplay.bean.DownLoadInfo;
import com.lizonghui.googleplay.bean.ItemInfoBean;
import com.lizonghui.googleplay.control.ItemHolder;
import com.lizonghui.googleplay.factory.ListViewFactory;
import com.lizonghui.googleplay.manager.DownLoadManger;
import com.lizonghui.googleplay.protocol.GameProtocol;

import java.io.IOException;
import java.util.List;

/**
 * Created by Administrator on 2016/6/17.
 * 1.initData 获得协议请求对象，请求数据，得到数据，校验数据
 * 2.listView创建设配器,复写时候有更多，加载更多数据
 * 3.得到holder
 */
public class GameFragment extends BaseFragment {

    private List<ItemInfoBean> mItemInfoBeans;
    private GameProtocol mGameProtocol;
    private GameAdapter mAdapter;

    @Override
    public View initSuccessView() {
        ListView listView = ListViewFactory.createListView();
        mAdapter = new GameAdapter(listView, mItemInfoBeans);
        listView.setAdapter(mAdapter);
        return listView;
    }

    @Override
    public LoadingPager.LoadedResultState initData() {
        SystemClock.sleep(2000);// 模拟网络耗时2s种
        /**
         返回值有几种情况:
         成功,失败,空
         */
        //得到协议请求对象
        mGameProtocol = new GameProtocol();
        try {
            //请求数据，得到数据
            mItemInfoBeans = mGameProtocol.loadData(0);
            //校验数据
           return checkResData(mItemInfoBeans);
        } catch (IOException e) {
            e.printStackTrace();
            return LoadingPager.LoadedResultState.ERROR;
        }

    }

    private class GameAdapter extends ItemAdapter{
        public GameAdapter(AbsListView absListView, List datas) {
            super(absListView, datas);
        }

        @Override
        public List initLoadMoreData() throws Exception {
            List<ItemInfoBean> itemInfoBeans = mGameProtocol.loadData(mItemInfoBeans.size());
            return itemInfoBeans;
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        //移除所有观察者,观察者在holder中建立，并通过adapter返回给ListView.所以用adapter作为判断位
        if(mAdapter != null){
            List<ItemHolder> itemHolders = mAdapter.itemHolders;
            for (ItemHolder itemHolder:itemHolders) {
                DownLoadManger.getInstance().deleteObserver(itemHolder);
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        //添加所有观察者
        if(mAdapter != null){
            List<ItemHolder> itemHolders = mAdapter.itemHolders;
            for (ItemHolder itemHolder:itemHolders) {
                DownLoadManger.getInstance().addObserver(itemHolder);


                //根据itemHolder的数据，得到downLoadInfo
                DownLoadInfo downLoadInfo = DownLoadManger.getInstance().getDownLoadInfo(itemHolder.mInfoBean);
                //手动发布最新消息，让UI刷新
                DownLoadManger.getInstance().notifyObservers(downLoadInfo);//要拿到downLoadInfo,而我们有
            }
        }
    }
}
